package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IItemService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.ProductCateResponse;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import com.mall.user.annotation.Anoymous;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @program: gateway
 * @description: 查看商品分类及商品详细信息
 * @author: Liu
 * @create: 2021-08-25 10:57
 **/

@Slf4j
@RestController
@RequestMapping("/shopping")
public class ItemController {

    @Reference(timeout = 3000, retries = 2, check = false)
    IItemService itemService;

    /**
     * 查看商品分类
     */
    @Anoymous
    @GetMapping("/categories")
    public ResponseData itemCat() {
//        AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
//        allProductCateRequest.setSort(sort);
        ProductCateResponse productCateResponse = itemService.itemCat();

        // 判断是否查询成功
        if (productCateResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            ResponseData success = new ResponseUtil().setData(productCateResponse.getProductCateDtoList(), "success");
            success.setTimestamp(System.currentTimeMillis());
            return success;
        } else {

            return new ResponseUtil().setErrorMsg("系统错误");
        }
    }

    /**
     * 查看商品详情
     */
    @Anoymous
    @GetMapping("/product/{id}")
    public ResponseData itemDetail(@PathVariable Long id) {
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);

        // 请求参数校验
        productDetailRequest.requestCheck();

        // service处理
        ProductDetailResponse response = itemService.itemDetail(productDetailRequest);
        // 判断是否查询成功
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            ResponseData success = new ResponseUtil().setData(response.getProductDetailDto(), "success");
            success.setTimestamp(System.currentTimeMillis());
            return success;
        } else {
            return new ResponseUtil().setErrorMsg("系统错误");

        }
    }
}
